package main.java;

import main.java.fireStrategy.FireStrategy;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class TankFrame extends Frame {
    Tank myTank = new Tank(50, 200, Dir.DOWN, this, Group.GOOD);
    List<Bullet> bullets = new ArrayList<>();
    List<Tank> tanks = new ArrayList<>();
    List<Explode> explodes = new ArrayList<>();
//    static int GAME_WIDTH = Integer.parseInt((String)PropertiesMgr.get("gameWidth"));
    static int GAME_WIDTH = Integer.parseInt((String) Objects.requireNonNull(PropertiesMgr.get("gameWidth")));
    static int GAME_HEIGHT = Integer.parseInt((String) Objects.requireNonNull(PropertiesMgr.get("gameHeight")));

    public TankFrame() throws HeadlessException {
        setSize(GAME_WIDTH,GAME_HEIGHT);
        setResizable(false);
        setTitle("tank war");
        setVisible(true);

        addKeyListener(new MyKeyListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


    }

    // 消除屏幕闪烁
    Image offScreenimage = null;

    @Override
    public void update(Graphics g) {
        if(offScreenimage == null) {
            offScreenimage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenimage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0,0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenimage,0, 0, null);
    }

    // 画面改变时会调用paint函数
    @Override
    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹数量" + bullets.size(),30,50);
        g.drawString("敌人数量" + tanks.size(),30,70);
        g.drawString("爆炸数量" + explodes.size(),30,90);
        g.setColor(c);
        myTank.paint(g);
        // 画出所有子弹
        for(int i = 0; i < bullets.size(); i++) {
            Bullet bullet = (Bullet) bullets.get(i);
            if(bullet.getX() < 0 || bullet.getX() > GAME_WIDTH || bullet.getY() < 0 || bullet.getY() > GAME_HEIGHT || !bullet.isAlive()) {
                bullets.remove(bullet);
            }
            bullet.paint(g);
        }
        // 画出所有坦克
        for(int i =0; i < tanks.size(); i++) {
            Tank tank = (Tank) tanks.get(i);
            if(!tank.getIsAlive()) {
                tanks.remove(tank);
            }
            tank.paint(g);
        }

        // 画出所有爆炸
        for(int i =0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }
        // 子弹碰撞检测
        for(int i = 0; i < bullets.size(); i++) {
            for(int j = 0; j < tanks.size(); j++) {
                Bullet bullet = (Bullet) bullets.get(i);
                bullet.collideWith((Tank) tanks.get(j));
            }
        }
    }

    // 键盘监听处理类
    class MyKeyListener extends KeyAdapter {
        boolean bU = false;
        boolean bD = false;
        boolean bL = false;
        boolean bR = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch(key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch(key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    String fs = (String) PropertiesMgr.get("goodFs");
                    try{
                        myTank.fire((FireStrategy) Class.forName(fs).getMethod("getInstance").invoke(Class.forName(fs)));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        private void setMainTankDir() {
            if(!bR && !bL && !bU && !bD) {
                myTank.setMoving(false);
            }
            else {
                myTank.setMoving(true);
                if(bL) {
                    myTank.setDir(Dir.LEFT);
                }
                if(bD) {
                    myTank.setDir(Dir.DOWN);
                }
                if(bR) {
                    myTank.setDir(Dir.RIGHT);
                }
                if(bU) {
                    myTank.setDir(Dir.UP);
                }
            }

        }
    }

}
